<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="500"
    class="label-add-or-update"
    @close="onCloseClr()"
  >
    <div class="edit-label-content">
      <div class="label-name-line currency-line">
        <div class="title">
          <span class="must-term">*</span>{{ $t("user.labelName") }}
        </div>
        <div>
          <el-input
            v-model="labelName"
            style="width:300px;height:30px;"
            type="text"
            maxlength="10"
            show-word-limit
          />
        </div>
      </div>
      <div class="label-type-line currency-line">
        <div class="title">
          <span class="must-term">*</span>{{ $t("user.labelType") }}
        </div>
        <el-radio
          v-model="labelType"
          label="1"
          :disabled="isDisaCondiLabel"
        >
          {{
            $t("user.conditionLabel")
          }}
        </el-radio>
        <el-radio
          v-model="labelType"
          label="0"
          :disabled="isDisaCustLabel"
        >
          {{
            $t("user.manualLabel")
          }}
        </el-radio>
      </div>
      <div v-if="labelType === '1'">
        <div class="label-condition-title-line">
          {{ $t("user.conditionSetting")
          }}<span class="tips">{{ $t("user.conditionSettingTips") }}</span>
        </div>
        <div class="basic-condition-line currency-line">
          <div class="title">
            {{ $t("user.basicCondition") }}
          </div>
          <div class="basic-set-content module-content">
            <div class="option-line">
              <el-checkbox v-model="isBeCustTimeChecked">
                {{
                  $t("user.becomeCustomerTime")
                }}
              </el-checkbox>
              <el-select
                v-model="beCustTimeValue"
                :disabled="!isBeCustTimeChecked"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in beCustTimeOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div
                v-if="isBeCustTimeChecked"
                class="block"
              >
                <el-date-picker
                  v-model="beCustTimeRange"
                  type="daterange"
                  range-separator="-"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  :start-placeholder="$t('user.startDate')"
                  :end-placeholder="$t('user.endDate')"
                  @change="onChagCustTimeRange(0)"
                />
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isBeMemTimeChecked">
                {{
                  $t("user.becomeMemberTime")
                }}
              </el-checkbox>
              <el-select
                v-model="beMemTimeValue"
                :disabled="!isBeMemTimeChecked"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in beMemTimeOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div
                v-if="isBeMemTimeChecked"
                class="block"
              >
                <el-date-picker
                  v-model="beMemTimeRange"
                  type="daterange"
                  range-separator="-"
                  value-format="YYYY-MM-DD HH:mm:ss"

                  :start-placeholder="$t('user.startDate')"
                  :end-placeholder="$t('user.endDate')"
                  @change="onChagCustTimeRange(2)"
                />
              </div>
            </div>
          </div>
        </div>
        <div class="transaction-condition-line currency-line">
          <div class="title">
            {{ $t("user.tradingConditions") }}
          </div>
          <div class="deal-condition-content module-content">
            <div class="option-line">
              <el-checkbox v-model="isNearConTime">
                {{
                  $t("user.nearConsuTime")
                }}
              </el-checkbox>
              <el-select
                v-model="nearConTimeValue"
                :disabled="!isNearConTime"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in nearConTimeOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isConNum">
                {{
                  $t("user.consuNum")
                }}
              </el-checkbox>
              <el-select
                v-model="conNumValue"
                :disabled="!isConNum"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in conNumOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="conNumBegInp"
                  type="number"
                  min="0"
                  max="100000"
                  :disabled="!isConNum"
                  class="cur-inp"
                  @change="onCheckNumber(1)"
                />
                <span class="unit">{{ $t("user.bout") }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="conNumEndInp"
                  type="number"
                  min="0"
                  max="100000"
                  :disabled="!isConNum"
                  class="cur-inp"
                  @change="onCheckNumber(2)"
                />
                <span class="unit">{{ $t("user.bout") }}</span>
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isConAmount">
                {{
                  $t("user.consuAmount")
                }}
              </el-checkbox>
              <el-select
                v-model="conAmountValue"
                :disabled="!isConAmount"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in conAmountOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="conAmountBegInp"
                  type="number"
                  min="0"
                  :disabled="!isConAmount"
                  class="cur-inp"
                  @change="onCheckNumber(3)"
                />
                <span class="unit">{{ $t("user.yuan") }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="conAmountEndInp"
                  type="number"
                  min="0"
                  :disabled="!isConAmount"
                  class="cur-inp"
                  @change="onCheckNumber(4)"
                />
                <span class="unit">{{ $t("user.yuan") }}</span>
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isOrderAverPrice">
                {{
                  $t("user.averPri")
                }}
              </el-checkbox>
              <el-select
                v-model="orderAverPriceValue"
                :disabled="!isOrderAverPrice"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in orderAverPriceOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="orderAverPriceBegInp"
                  type="number"
                  min="0"
                  :disabled="!isOrderAverPrice"
                  class="cur-inp"
                  @change="onCheckNumber(5)"
                />
                <span class="unit">{{ $t("user.yuan") }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="orderAverPriceEndInp"
                  type="number"
                  min="0"
                  :disabled="!isOrderAverPrice"
                  class="cur-inp"
                  @change="onCheckNumber(6)"
                />
                <span class="unit">{{ $t("user.yuan") }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="transaction-condition-line currency-line">
          <div class="title">
            {{ $t("user.rechargeConditions") }}
          </div>
          <div class="deal-condition-content module-content">
            <div class="option-line">
              <el-checkbox v-model="isRecharAmount">
                {{
                  $t("user.rechargeAmount")
                }}
              </el-checkbox>
              <el-select
                v-model="recharAmountValue"
                :disabled="!isRecharAmount"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in recharAmountOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="recharAmountBegInp"
                  type="number"
                  min="0"
                  :disabled="!isRecharAmount"
                  class="cur-inp"
                  @change="onCheckNumber(7)"
                />
                <span class="unit">{{ $t("user.yuan") }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="recharAmountEndInp"
                  type="number"
                  min="0"
                  :disabled="!isRecharAmount"
                  class="cur-inp"
                  @change="onCheckNumber(8)"
                />
                <span class="unit">{{ $t("user.yuan") }}</span>
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isRecharNum">
                {{
                  $t("user.rechargeNumber")
                }}
              </el-checkbox>
              <el-select
                v-model="recharNumValue"
                :disabled="!isRecharNum"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in recharNumOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="recharNumBegInp"
                  type="number"
                  min="0"
                  :disabled="!isRecharNum"
                  class="cur-inp"
                  @change="onCheckNumber(9)"
                />
                <span class="unit">{{ $t("user.bout") }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="recharNumEndInp"
                  type="number"
                  min="0"
                  :disabled="!isRecharNum"
                  class="cur-inp"
                  @change="onCheckNumber(10)"
                />
                <span class="unit">{{ $t("user.bout") }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 按键 -->
    <template #footer>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="dialogVisible = false"
        >
          {{ $t("user.calcel") }}
        </div>
        <div
          class="default-btn primary-btn"
          @click="onPreserveLabel"
        >
          {{ $t("user.preservation") }}
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import moment from 'moment'
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshLabelList', 'refreshLabelList', 'refreshLabelList'])

// 基础条件
const basicConOpts = [
  {
    value: 0,
    label: $t('user.today')
  },
  {
    value: 1,
    label: $t('user.yesterday')
  },
  {
    value: 2,
    label: $t('user.lastSevenDay')
  },
  {
    value: 3,
    label: $t('user.lastThirtyDay')
  },
  {
    value: 4,
    label: $t('user.thisMonth')
  },
  {
    value: 5,
    label: $t('user.lastMonth')
  },
  {
    value: 6,
    label: $t('user.customRange')
  }
]

// 交易条件
const dealConditionOpts = [
  {
    value: 1,
    label: $t('user.today')
  },
  {
    value: 2,
    label: $t('user.lastSevenDay')
  },
  {
    value: 3,
    label: $t('user.lastFifteenDay')
  },
  {
    value: 4,
    label: $t('user.lastThirtyDay')
  },
  {
    value: 5,
    label: $t('user.lastFortyFiveDay')
  },
  {
    value: 6,
    label: $t('user.lastSixtyDay')
  },
  {
    value: 7,
    label: $t('user.lastNinetyDay')
  },
  {
    value: 8,
    label: $t('user.lastOneHundredEightyDay')
  },
  {
    value: 0,
    label: $t('user.unlimited')
  }
]

const beCustTimeValue = ref(0)
watch(beCustTimeValue, () => {
  onChangeTimeRan(0)
})
const beMemTimeValue = ref(0)
watch(beMemTimeValue, () => {
  onChangeTimeRan(2)
})
onMounted(() => {
  onPullInitSelOpts()
  onChangeTimeRan(0)
  onChangeTimeRan(1)
  onChangeTimeRan(2)
})

// 弹出弹框
const isDisaCustLabel = ref(false)
let userTagId = ''
const title = ref($t('user.addLabel'))
const dialogVisible = ref(false)
const labelType = ref('1')
const isDisaCondiLabel = ref(false)
const init = (isUserTagId, tagType) => {
  dialogVisible.value = true
  userTagId = isUserTagId
  if (tagType === 0) {
    isDisaCondiLabel.value = true
    labelType.value = '0'
    if (userTagId !== '') {
      title.value = $t('user.editLabel')
      userTagId = isUserTagId
      onGetEditLabelDetail()
    }
  } else if (tagType === 1) {
    isDisaCustLabel.value = true
    labelType.value = '1'
    if (userTagId !== '') {
      title.value = $t('user.editLabel')
      userTagId = isUserTagId
      onGetEditLabelDetail()
    }
  }
}
/**
 * 输入框的数据改变时，对值进行校验
 */
const conNumBegInp = ref('')
const conNumEndInp = ref('')
const conAmountBegInp = ref('')
const conAmountEndInp = ref('')
const orderAverPriceBegInp = ref('')
const orderAverPriceEndInp = ref('')
const recharAmountBegInp = ref('')
const recharAmountEndInp = ref('')
// 充值次数
const isRecharNum = ref(false)
const recharNumBegInp = ref('')
const recharNumEndInp = ref('')

const onCheckNumber = (type) => {
  // 消费次数
  if (type === 1) {
    let BegNum = Math.round(parseInt(conNumBegInp.value))
    if (!BegNum) {
      BegNum = 0
    } else if (BegNum < 0) {
      BegNum = 0
    } else if (BegNum > 100000) {
      BegNum = 100000
    }
    conNumBegInp.value = BegNum
  }
  if (type === 2) {
    let endNum = Math.round(parseInt(conNumEndInp.value))
    if (!endNum) {
      endNum = 0
    } else if (endNum < 0) {
      endNum = 0
    } else if (endNum > 100000) {
      endNum = 100000
    }
    conNumEndInp.value = endNum
  }
  // 消费金额
  if (type === 3) {
    let conBegAmountNum = Math.round(
      parseFloat(conAmountBegInp.value).toFixed(2)
    )
    if (!conBegAmountNum) {
      conBegAmountNum = 0
    } else if (conBegAmountNum < 0) {
      conBegAmountNum = 0
    } else if (conBegAmountNum >= 100000000) {
      conBegAmountNum = 100000000
    }
    conAmountBegInp.value = conBegAmountNum
  }
  if (type === 4) {
    let conEndAmountNum = Math.round(
      parseFloat(conAmountEndInp.value).toFixed(2)
    )
    if (!conEndAmountNum) {
      conEndAmountNum = 0
    } else if (conEndAmountNum < 0) {
      conEndAmountNum = 0
    } else if (conEndAmountNum >= 100000000) {
      conEndAmountNum = 100000000
    }
    conAmountEndInp.value = conEndAmountNum
  }
  // 订单均价
  if (type === 5) {
    let ordAverPriceBegNum = Math.round(
      parseFloat(orderAverPriceBegInp.value).toFixed(2)
    )
    if (!ordAverPriceBegNum) {
      ordAverPriceBegNum = 0
    } else if (ordAverPriceBegNum < 0) {
      ordAverPriceBegNum = 0
    } else if (ordAverPriceBegNum >= 100000000) {
      ordAverPriceBegNum = 100000000
    }
    orderAverPriceBegInp.value = ordAverPriceBegNum
  }
  if (type === 6) {
    let ordAverPriceEndNum = Math.round(
      parseFloat(orderAverPriceEndInp.value).toFixed(2)
    )
    if (!ordAverPriceEndNum) {
      ordAverPriceEndNum = 0
    } else if (ordAverPriceEndNum < 0) {
      ordAverPriceEndNum = 0
    } else if (ordAverPriceEndNum >= 100000000) {
      ordAverPriceEndNum = 100000000
    }
    orderAverPriceEndInp.value = ordAverPriceEndNum
  }
  // 充值金额
  if (type === 7) {
    let reAmountBegNum = Math.round(
      parseFloat(recharAmountBegInp.value).toFixed(2)
    )
    if (!reAmountBegNum) {
      reAmountBegNum = 0
    } else if (reAmountBegNum < 0) {
      reAmountBegNum = 0
    } else if (reAmountBegNum >= 100000000) {
      reAmountBegNum = 100000000
    }
    recharAmountBegInp.value = reAmountBegNum
  }
  if (type === 8) {
    let reAmountEndNum = Math.round(
      parseFloat(recharAmountEndInp.value).toFixed(2)
    )
    if (!reAmountEndNum) {
      reAmountEndNum = 0
    } else if (reAmountEndNum < 0) {
      reAmountEndNum = 0
    } else if (reAmountEndNum >= 100000000) {
      reAmountEndNum = 100000000
    }
    recharAmountEndInp.value = reAmountEndNum
  }
  // 充值次数
  if (type === 9) {
    let recharBegNum = Math.round(parseInt(recharNumBegInp.value))
    if (!recharBegNum) {
      recharBegNum = 0
    } else if (recharBegNum < 0) {
      recharBegNum = 0
    } else if (recharBegNum > 100000) {
      recharBegNum = 100000
    }
    recharNumBegInp.value = recharBegNum
  }
  if (type === 10) {
    let recharEndNum = Math.round(parseInt(recharNumEndInp.value))
    if (!recharEndNum) {
      recharEndNum = 0
    } else if (recharEndNum < 0) {
      recharEndNum = 0
    } else if (recharEndNum > 100000) {
      recharEndNum = 100000
    }
    recharNumEndInp.value = recharEndNum
  }
}
const beCustTimeOpts = ref([])
const beCustTimeRange = ref('')
const beMemTimeOpts = ref([])
// 最近消费时间
const nearConTimeOpts = ref([])
const conNumOpts = ref([])
const conAmountOpts = ref([])
// 订单均价
const orderAverPriceOpts = ref([])
const recharAmountOpts = ref([])
const recharNumOpts = ref([])

const onPullInitSelOpts = () => {
  // 基础条件
  beCustTimeOpts.value = basicConOpts
  beMemTimeOpts.value = basicConOpts
  // 交易条件
  nearConTimeOpts.value = dealConditionOpts
  conNumOpts.value = dealConditionOpts
  conAmountOpts.value = dealConditionOpts
  orderAverPriceOpts.value = dealConditionOpts
  recharAmountOpts.value = dealConditionOpts
  recharNumOpts.value = dealConditionOpts
}
// 获取单个标签的详情
const labelName = ref('')
let followTimeRange = []
const isNearConTime = ref(false)
let isfollowTimeChecked = false
// 成为客户时间
const isBeCustTimeChecked = ref(false)
// 成为会员时间
const isBeMemTimeChecked = ref(false)
const beMemTimeRange = ref('')
const nearConTimeValue = ref(1)
// 消费次数
const isConNum = ref(false)
const conNumValue = ref(1)
// 消费金额
const isConAmount = ref(false)
const conAmountValue = ref(1)
const isOrderAverPrice = ref(false)
const orderAverPriceValue = ref(1)
// 充值金额
const isRecharAmount = ref(false)
const recharAmountValue = ref(1)
const recharNumValue = ref(1)

const onGetEditLabelDetail = () => {
  http({
    url: http.adornUrl('/user/userTag/info/' + userTagId),
    method: 'GET',
    data: http.adornData({})
  }).then(({ data }) => {
    if (labelType.value === '0') {
      labelName.value = data.tagName
    } else if (labelType.value === '1') {
      labelName.value = data.tagName
      // 成为客户时间
      if (data.registerMinTime !== null && data.registerMaxTime !== null) {
        const isBeCustTimeRange = []
        isBeCustTimeChecked.value = true
        beCustTimeValue.value = 6
        beCustTimeRange.value.push(data.registerMinTime)
        beCustTimeRange.value.push(data.registerMaxTime)
        beCustTimeRange.value = isBeCustTimeRange
      }
      // 关注时间
      if (
        data.subscribeMinTime !== null &&
        data.subscribeMaxTime !== null
      ) {
        const isFollowTimeRange = []
        isfollowTimeChecked = true
        isFollowTimeRange.push(data.subscribeMinTime)
        isFollowTimeRange.push(data.subscribeMaxTime)
        followTimeRange = isFollowTimeRange
      }
      // 成为会员时间
      if (
        data.toBeMemberMinTime !== null &&
        data.toBeMemberMaxTime !== null
      ) {
        const isBeMemTimeRange = []
        isBeMemTimeChecked.value = true
        beMemTimeValue.value = 6
        beMemTimeRange.value.push(data.toBeMemberMinTime)
        beMemTimeRange.value.push(data.toBeMemberMaxTime)
        beMemTimeRange.value = isBeMemTimeRange
      }
      // 最近消费时间
      if (data.recentPurchaseTime !== null) {
        isNearConTime.value = true
        nearConTimeValue.value = data.recentPurchaseTime
      }
      // 消费次数
      if (
        data.purchaseNumTime !== null &&
        data.purchaseNumMinNum !== null &&
        data.purchaseNumMaxNum !== null
      ) {
        isConNum.value = true
        conNumValue.value = data.purchaseNumTime
        conNumBegInp.value = data.purchaseNumMinNum
        conNumEndInp.value = data.purchaseNumMaxNum
      }
      // 消费金额
      if (
        data.purchaseAmountTime !== null &&
        data.purchaseAmountMinAmount !== null &&
        data.purchaseAmountMaxAmount !== null
      ) {
        isConAmount.value = true
        conAmountValue.value = data.purchaseAmountTime
        conAmountBegInp.value = data.purchaseAmountMinAmount
        conAmountEndInp.value = data.purchaseAmountMaxAmount
      }
      // 订单均价
      if (
        data.orderAveragePriceTime !== null &&
        data.orderAveragePriceMinAmount !== null &&
        data.orderAveragePriceMaxAmount !== null
      ) {
        isOrderAverPrice.value = true
        orderAverPriceValue.value = data.orderAveragePriceTime
        orderAverPriceBegInp.value = data.orderAveragePriceMinAmount
        orderAverPriceEndInp.value = data.orderAveragePriceMaxAmount
      }
      // 充值金额
      if (
        data.rechargeAmountTime !== null &&
        data.rechargeAmountMinAmount !== null &&
        data.rechargeAmountMaxAmount !== null
      ) {
        isRecharAmount.value = true
        recharAmountValue.value = data.rechargeAmountTime
        recharAmountBegInp.value = data.rechargeAmountMinAmount
        recharAmountEndInp.value = data.rechargeAmountMaxAmount
      }
      // 充值次数
      if (
        data.rechargeNumTime !== null &&
        data.rechargeNumMinNum !== null &&
        data.rechargeNumMaxNum !== null
      ) {
        isRecharNum.value = true
        recharNumValue.value = data.rechargeNumTime
        recharNumBegInp.value = data.rechargeNumMinNum
        recharNumEndInp.value = data.rechargeNumMaxNum
      }
    }
  })
}
// 修改为自定义范围
const onChagCustTimeRange = (val) => {
  // val：0-成为客户时间 1-关注时间 2-成为会员时间
  if (val === 0) {
    beCustTimeValue.value = 6
  } else if (val === 2) {
    beMemTimeValue.value = 6
  }
}
// 改变时间范围
const onChangeTimeRan = (value) => {
  // value: 0-成为客户时间 1-关注时间 2-成为会员时间
  let seleItem = ''
  const timeRange = []
  const time = new Date()
  let beginTime = ''
  let endTime = ''
  if (value === 0) {
    seleItem = beCustTimeValue.value
  } else if (value === 2) {
    seleItem = beMemTimeValue.value
  }

  if (seleItem === 0) {
    // 今天
    beginTime = moment(time).format('LL')
    endTime = beginTime
  } else if (seleItem === 1) {
    // 昨天
    beginTime = moment(time).add(-1, 'days').format('LL')
    endTime = beginTime
  } else if (seleItem === 2) {
    // 近7天
    beginTime = moment(time).add(-7, 'days').format('LL')
    endTime = moment(time).add(-1, 'days').format('LL')
  } else if (seleItem === 3) {
    // 近30天
    beginTime = moment(time).add(-30, 'days').format('LL')
    endTime = moment(time).add(-1, 'days').format('LL')
  } else if (seleItem === 4) {
    // 本月的开始结束时间
    beginTime = moment(time).startOf('month').format('LL')
    endTime = moment(time).endOf('month').format('LL')
  } else if (seleItem === 5) {
    // 上个月的开始结束时间
    beginTime = moment(time).add(-1, 'month').startOf('month').format('LL')
    endTime = moment(time).add(-1, 'month').endOf('month').format('LL')
  }
  if (seleItem !== 6) {
    timeRange.push(beginTime)
    timeRange.push(endTime)
    if (value === 0) {
      beCustTimeRange.value = timeRange
    } else if (value === 2) {
      beMemTimeRange.value = timeRange
    }
  }
}
// 增加/编辑标签
// 消费次数
let purchaseNumTime = ''
let purchaseNumMinNum = ''
let purchaseNumMaxNum = ''
// 消费金额
let purchaseAmountTime = ''
let purchaseAmountMinAmount = ''
let purchaseAmountMaxAmount = ''
// 订单均价
let orderAveragePriceTime = ''
let orderAveragePriceMinAmount = ''
let orderAveragePriceMaxAmount = ''
// 充值金额
let rechargeAmountTime = ''
let rechargeAmountMinAmount = ''
let rechargeAmountMaxAmount = ''
let rechargeNumTime = ''
let rechargeNumMinNum = ''
let rechargeNumMaxNum = ''
// 成为客户时间
let registerMinTime = ''
let registerMaxTime = ''
const onPreserveLabel = () => {
  // 判断是否为整数/零的正则
  const posIntReg = /^([0]|[1-9][0-9]*)$/
  if (!labelName.value) {
    ElMessage.error($t('product.labelNameCannotBeEmpty'))
    return
  }
  if (!labelName.value || !labelName.value.trim()) {
    ElMessage.error($t('product.labelNameNotBlankSpace'))
    return
  }
  // 新增条件标签
  if (labelType.value === '0') {
    // 判断是新增/编辑
    const addOrEditMeth = userTagId !== '' ? 'PUT' : 'POST'
    const params = {
      userTagId,
      tagType: labelType.value,
      tagName: labelName.value
    }
    http({
      url: http.adornUrl('/user/userTag'),
      method: addOrEditMeth,
      data: http.adornData(params)
    }).then(() => {
      dialogVisible.value = false
      emit('refreshLabelList', true)
    })
  } else if (labelType.value === '1') {
    // 清空条件的参数标记
    let clearRegisterTime = false
    let clearSubscribeTime = false
    let clearToBeMemberTime = false
    let clearRecentPurchaseTime = false
    let clearPurchaseNum = false
    let clearPurchaseAmount = false
    let clearOrderAveragePrice = false
    let clearRechargeAmount = false
    let clearRechargeNum = false
    // 判断是新增/编辑
    const addOrEditMeth = userTagId !== '' ? 'PUT' : 'POST'
    // 判断是否选中某个条件，选中就就不能为空
    isBeCustTimeChecked.value === true &&
    beCustTimeRange.value !== null &&
    beCustTimeRange.value[0] !== '' &&
    beCustTimeRange.value[1] !== '' ? registerMinTime = moment(beCustTimeRange.value[0]).startOf('days').format('LL') : registerMinTime = moment(beCustTimeRange.value[0]).startOf('days').format('LL')
    registerMaxTime = moment(beCustTimeRange.value[1]).endOf('days').format('LL')
    clearRegisterTime = !isBeCustTimeChecked.value ? true : clearRegisterTime
    // 关注时间
    let subscribeMinTime = ''
    let subscribeMaxTime = ''
    subscribeMinTime = isfollowTimeChecked && followTimeRange != null && followTimeRange[0] !== '' && followTimeRange[1] !== '' ? moment(followTimeRange[0]).startOf('days').format('LL') : subscribeMinTime
    subscribeMaxTime = isfollowTimeChecked && followTimeRange != null && followTimeRange[0] !== '' && followTimeRange[1] !== '' ? moment(followTimeRange[1]).endOf('days').format('LL') : subscribeMaxTime
    clearSubscribeTime = !isfollowTimeChecked ? true : clearSubscribeTime
    // 成为会员时间
    let toBeMemberMinTime = ''
    let toBeMemberMaxTime = ''
    toBeMemberMinTime = isBeMemTimeChecked.value && beMemTimeRange.value != null && beMemTimeRange.value[0] !== '' && beMemTimeRange.value[1] !== '' ? moment(beMemTimeRange.value[0]).startOf('days').format('LL') : toBeMemberMinTime
    toBeMemberMaxTime = isBeMemTimeChecked.value && beMemTimeRange.value != null && beMemTimeRange.value[0] !== '' && beMemTimeRange.value[1] !== '' ? moment(beMemTimeRange.value[1]).endOf('days').format('LL') : toBeMemberMaxTime
    clearToBeMemberTime = !isBeMemTimeChecked.value ? true : clearToBeMemberTime
    // 最近消费时间
    let recentPurchaseTime = ''
    isNearConTime.value ? recentPurchaseTime = nearConTimeValue.value : clearRecentPurchaseTime = true
    // 消费次数
    purchaseNumTime = isConNum.value ? conNumValue.value : purchaseNumTime
    purchaseNumMinNum = isConNum.value && posIntReg.test(conNumBegInp.value) ? conNumBegInp.value : purchaseNumMinNum
    purchaseNumMaxNum = isConNum.value && posIntReg.test(conNumEndInp.value) ? conNumEndInp.value : purchaseNumMaxNum
    clearPurchaseNum = !isConNum.value ? true : clearPurchaseNum
    // 消费金额
    purchaseAmountTime = isConAmount.value ? conAmountValue.value : purchaseAmountTime
    purchaseAmountMinAmount = (isConAmount.value && posIntReg.test(conAmountBegInp.value) && posIntReg.test(conAmountEndInp.value)) ? conAmountBegInp.value : purchaseAmountMinAmount
    purchaseAmountMaxAmount = (isConAmount.value && posIntReg.test(conAmountEndInp.value) && posIntReg.test(conAmountBegInp.value)) ? conAmountEndInp.value : purchaseAmountMaxAmount
    clearPurchaseAmount = !isConAmount.value ? true : clearPurchaseAmount
    // 订单均价
    orderAveragePriceTime = isOrderAverPrice.value ? orderAverPriceValue.value : orderAveragePriceTime
    orderAveragePriceMinAmount = (isOrderAverPrice.value && posIntReg.test(orderAverPriceBegInp.value) && posIntReg.test(orderAverPriceEndInp.value)) ? orderAverPriceBegInp.value : orderAveragePriceMinAmount
    orderAveragePriceMaxAmount = (isOrderAverPrice.value && posIntReg.test(orderAverPriceBegInp.value) && posIntReg.test(orderAverPriceEndInp.value)) ? orderAverPriceEndInp.value : orderAveragePriceMaxAmount
    clearOrderAveragePrice = !isOrderAverPrice.value ? true : clearOrderAveragePrice
    // 充值金额
    rechargeAmountTime = isRecharAmount.value ? recharAmountValue.value : rechargeAmountTime
    rechargeAmountMinAmount = (isRecharAmount.value && posIntReg.test(recharAmountBegInp.value) && posIntReg.test(recharAmountEndInp.value)) ? recharAmountBegInp.value : rechargeAmountMinAmount
    rechargeAmountMaxAmount = (isRecharAmount.value && posIntReg.test(recharAmountBegInp.value) && posIntReg.test(recharAmountEndInp.value)) ? recharAmountEndInp.value : rechargeAmountMaxAmount
    clearRechargeAmount = !isRecharAmount.value ? true : clearRechargeAmount
    // 充值次数
    rechargeNumTime = isRecharNum.value ? recharNumValue.value : rechargeNumTime
    rechargeNumMinNum = (isRecharNum.value && posIntReg.test(recharNumBegInp.value) && posIntReg.test(recharNumEndInp.value)) ? recharNumBegInp.value : rechargeNumMinNum
    rechargeNumMaxNum = (isRecharNum.value && posIntReg.test(recharNumBegInp.value) && posIntReg.test(recharNumEndInp.value)) ? recharNumEndInp.value : rechargeNumMaxNum
    clearRechargeNum = !isRecharNum.value ? true : clearRechargeNum
    const params = {
      userTagId,
      tagType: labelType.value,
      tagName: labelName.value,
      registerMinTime,
      registerMaxTime,
      subscribeMinTime,
      subscribeMaxTime,
      toBeMemberMinTime,
      toBeMemberMaxTime,
      recentPurchaseTime,
      purchaseNumTime,
      purchaseNumMinNum,
      purchaseNumMaxNum,
      purchaseAmountTime,
      purchaseAmountMinAmount,
      purchaseAmountMaxAmount,
      orderAveragePriceTime,
      orderAveragePriceMinAmount,
      orderAveragePriceMaxAmount,
      rechargeAmountTime,
      rechargeAmountMinAmount,
      rechargeAmountMaxAmount,
      rechargeNumTime,
      rechargeNumMinNum,
      rechargeNumMaxNum,
      clearRegisterTime,
      clearSubscribeTime,
      clearToBeMemberTime,
      clearRecentPurchaseTime,
      clearPurchaseNum,
      clearPurchaseAmount,
      clearOrderAveragePrice,
      clearRechargeAmount,
      clearRechargeNum
    }
    // 标签名称
    labelName.value.trim() === '' ? ElMessage.warning($t('user.labelNameNullTips')) : (!isBeCustTimeChecked.value &&
!isBeMemTimeChecked.value &&
!isConNum.value &&
!isConAmount.value &&
!isOrderAverPrice.value &&
!isRecharNum.value &&
!isNearConTime.value &&
!isRecharAmount.value) ? ElMessage.warning($t('user.requireOne')) : isBeCustTimeChecked.value &&
(beCustTimeRange.value === null ||
beCustTimeRange.value[0] === '' ||
beCustTimeRange.value[1] === '') ? ElMessage.warning($t('user.becomeCustomerTimeNullTips')) : isfollowTimeChecked &&
(followTimeRange === null ||
followTimeRange[0] === '' ||
followTimeRange[1] === '') ? ElMessage.warning($t('user.followTimeNullTips')) : isBeMemTimeChecked.value &&
(beMemTimeRange.value === null ||
beMemTimeRange.value[0] === '' ||
beMemTimeRange.value[1] === '') ? ElMessage.warning($t('user.becomeMemberTimeNullTips')) : isConNum.value && (purchaseNumMinNum === '' || purchaseNumMaxNum === '') ? ElMessage.warning($t('user.consuNumNullTips')) : isConAmount.value && (purchaseAmountMinAmount === '' || purchaseAmountMaxAmount === '') ? ElMessage.warning($t('user.consuAmountNullTips')) : isOrderAverPrice.value &&
(orderAveragePriceMinAmount === '' ||
orderAveragePriceMaxAmount === '') ? ElMessage.warning($t('user.averPriNullTips')) : isRecharAmount.value && (rechargeAmountMinAmount === '' || rechargeAmountMaxAmount === '') ? ElMessage.warning($t('user.rechargeAmountNullTips')) : isRecharNum.value && (rechargeNumMinNum === '' || rechargeNumMaxNum === '') ? ElMessage.warning($t('user.rechargeNumberNullTips')) : onSubmit(params, addOrEditMeth)
  }
}
const onSubmit = (params, addOrEditMeth) => {
  http({
    url: http.adornUrl('/user/userTag'),
    method: addOrEditMeth,
    data: http.adornData(params)
  }).then(() => {
    dialogVisible.value = false
    emit('refreshLabelList', true)
  })
}
const onCloseClr = () => {
  emit('refreshLabelList', false)
}
defineExpose({ init })
</script>

<style lang="scss" scoped>
.label-add-or-update{
  .edit-label-content {
    :deep(.el-input){
      height: 40px;
    }
  font-size: 12px;
  /* 行内通用 */
  .currency-line {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }
  .currency-line .title {
    width: 140px;
    font-size: 12px;
    font-weight: normal;
    text-align: right;
    padding: 0 15px 0 0;
    margin: 0;
  }
  .currency-line .must-term {
    color: #f56c6c;
    margin-right: 4px;
  }
  .currency-line :deep(.el-checkbox)  {
    width: 170px;
  }
  :deep(.el-checkbox__input)  {
    float: left;
  }
  .currency-line :deep(.el-radio__label) ,
  .currency-line :deep(.el-checkbox__label) ,
  .currency-line :deep(.el-input) ,
  .currency-line :deep(.el-range-input)  {
    font-size: 12px;
    white-space: normal;
    width: 83%;
  }
  // .currency-line .label-condition-title-line :deep(.el-input){
  //   height: 40px !important;
  // }
  .currency-line .module-content {
    padding: 0 15px;
  }
  .currency-line .module-content .option-line {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
  }
  .currency-line .module-content .option-line .unit-wrapper {
    display: flex;
    align-items: center;
  }
  .currency-line .module-content .option-line .first-iterm {
    margin-left: 25px;
  }
  .currency-line .module-content .option-line .interval-line {
    padding: 0 10px;
  }
  .currency-line .module-content .option-line .unit {
    display: inline-block;
    width: 50px;
    height: 40px;
    line-height: 40px;
    background: #efefef;
    text-align: center;
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
  }
  .currency-line .module-content :deep(.el-select)  {
    width: 185px;
    height: 38px;
    margin-right: 15px;
  }
  .currency-line .module-content .cur-inp {
    width: 130px;
  }
  .currency-line .module-content .cur-inp :deep(.el-input__inner) {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }
  /* /行内通用 */
  .label-condition-title-line {
    display: flex;
    align-items: center;
    font-size: 14px;
    font-weight: 700;
    margin-left: 10px;
    margin-bottom: 10px;
  }
  .label-condition-title-line .tips {
    font-size: 12px;
    font-weight: normal;
    color: #595961;
    padding-left: 20px;
  }
}
}

</style>
